package com.linkin.homepage.ui.home.homeclass;


import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.linkin.homepage.R;
import com.linkin.homepage.base.BaseFragment;
import com.linkin.homepage.base.recycleview.RCWrapperAdapter;
import com.linkin.homepage.base.rxbase.RxManager;
import com.linkin.homepage.base.viewmapping.ViewMapping;
import com.linkin.homepage.bean.ClassItemBean;
import com.linkin.homepage.ui.adapter.ComRCAdapter;
import com.linkin.homepage.ui.home.ComListHelper;
import com.linkin.homepage.ui.home.OnReClickListener;
import com.linkin.homepage.view.refresh.RefreshFooterState;
import com.linkin.homepage.view.refresh.RefreshViewUtils;

import java.util.List;

@ViewMapping(R.layout.fragment_class_content)
public class ClassFragment extends BaseFragment implements ClassContract.View, OnReClickListener {

    private RecyclerView mRecyclerView;
    private ComRCAdapter mNewsAdapter;
    private RxManager mRxManager;


    private ClassPresenter mPresenter;

    private String mExamId;

    public static ClassFragment newInstance(String examId) {
        ClassFragment fragment = new ClassFragment();
        fragment.mExamId = examId;
        return fragment;
    }

    @Override
    protected void initView(View rootView) {
        initPresenter();
        initRxManager();
        initRecyclerView();
    }

    private void initPresenter() {
        mPresenter = new ClassPresenter(this, mExamId);
    }

    private void initRxManager() {
        mRxManager = new RxManager();
    }

    private void initRecyclerView() {
        mRecyclerView = getRootView().findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));

        mNewsAdapter = new ComRCAdapter(mContext);
        mNewsAdapter.setRxManager(mRxManager);
        mRecyclerView.setAdapter(new RCWrapperAdapter(mNewsAdapter));

        RefreshViewUtils.setOnRefreshListener(mRecyclerView, this::onRefresh);
        RefreshViewUtils.setOnLoadMoreListener(mRecyclerView, this::onLoadMore);
    }

    @Override
    protected void initData() {
        super.initData();
        mPresenter.subscribe();
    }

    private void onRefresh(boolean isGesture) {

    }

    private void onLoadMore() {

    }

    @Override
    protected void refreshData() {
        if (isAlive()) {
            ComListHelper.refreshData(this, mRecyclerView, mNewsAdapter);
        }
    }

    @Override
    protected void clear() {
        mRxManager.clear();
    }

    @Override
    public void onReClick() {

    }

    @Override
    public void showClassItem(List<ClassItemBean> classItemBeanList) {
        if (isFragmentDestroyed()) {
            return;
        }

        showNormal();
        //下拉刷新重置
        RefreshViewUtils.onRefreshComplete(mRecyclerView);
        //加载更多重置
        RefreshViewUtils.onLoadMoreComplete(mRecyclerView, RefreshFooterState.GONE);

        mNewsAdapter.updateList(classItemBeanList);
    }

    @Override
    public void showError(boolean refresh) {

    }
}
